home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
9-Digit Zip Code Directory
/
9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO
/
z4src.zip
/
BSFILE1.C
< prev
next >
Wrap
C/C++ Source or Header
|
1995-09-20
|
6KB
|
236 lines
//----------------------------------------------------------------------------
// MODULE DESCRIPTION
//
// Module: bsfile1.c
// Title: Base library
// Notice: John M. Weeder
// Copyright (c) 1993. All rights reserved.
// This module contains proprietary information and should be
// treated as confidential.
//
//----------------------------------------------------------------------------
// MAINTENANCE HISTORY
//
// $Workfile$
// $Revision$
// $Author$
// $Date$
// $Log$
//
//----------------------------------------------------------------------------
// MODULE NARRATIVE
//
//
// This module contains .
//
// The code in this module should be written entirely in C.
// Do not use any C++ constructs.
//
// This module is portable to:
// DOS 3.X+
// MS Windows 3.X+
// MS Windows NT
// OS/2 2.X+
// OS/2 2.0 PM
// SCO UNIX.
//
// The following compilers are supported:
// MSC 6.0A
// MSC/C++ 7.0
// Borland C++ 3.1 for DOS
// Borland C++ 1.0 for OS/2 2.X
// SCO UNIX cc
//
//----------------------------------------------------------------------------
#include <bs.h>
//----------------------------------------------------------------------------
// Description: Copy data between two files.
// Parameters: hf1 File handle 1.
// File to copy TO
// fpos1 Position to start copying at.
// If -1, copy starting at current position.
// hf2 File handle 2.
// File to copy FROM
// fpos2 Position to start copying at.
// If -1, copy starting at current position.
// flen Length of file to copy.
// If < 0, copy to end of input file.
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
BOOL FN_E FileCopy(HF hf1, FPOS fpos1, HF hf2, FPOS fpos2, FPOS flen)
{
FPOS fsize;
SIZET cb;
PBYTE pbBuf = NULL;
BOOL fResult = FALSE;
if (fpos1 < 0)
fpos1 = FileGetPos(hf1);
if (fpos1 < 0)
return FALSE;
if (fpos2 < 0)
fpos2 = FileGetPos(hf2);
if (fpos2 < 0)
return FALSE;
fsize = FileGetSize(hf2);
if (fsize < 0)
return FALSE;
if (fpos2 >= fsize) // Copying from past eof?
return TRUE;
if (flen < 0)
flen = fsize - fpos2;
flen = MIN(flen, fsize - fpos2);
if (flen <= 0) // Nothing to copy?
return TRUE;
// Allocate a copy buffer
cb = (SIZET)MIN((FPOS)(16 _K), flen);
pbBuf = MemAlloc(cb);
if (pbBuf == NULL)
{
ErrorNoMem();
goto ERROR_EXIT;
}
while (flen) // Copy file
{
SIZET cCopy = (SIZET)MIN((FPOS)cb, flen);
if (!FileRead(hf2, pbBuf, cCopy, fpos2))
goto ERROR_EXIT;
if (!FileWrite(hf1, pbBuf, cCopy, fpos1))
goto ERROR_EXIT;
flen -= (FPOS)cCopy;
fpos1 += (FPOS)cCopy;
fpos2 += (FPOS)cCopy;
}
fResult = TRUE;
ERROR_EXIT:
if (pbBuf)
MemFree(pbBuf);
return fResult;
}
//----------------------------------------------------------------------------
// Description: Compute the CRC of a file.
// Parameters: hf File handle
// pcrc Variable to recieve crc
// flen Length of file to compute CRC for.
// Length is adjust to fit file size.
// If < 0, calculate remaing length of file.
// fpos Address to start computing at.
// If < 0, start at current position.
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
BOOL FN_E FileCrc(HF hf, PCRC pcrc, FPOS flen, FPOS fpos)
{
SIZET cBuf;
FPOS fsize;
SIZET cb;
PBYTE pbBuf = NULL;
BOOL fResult = FALSE;
Assert(pcrc);
if (fpos < 0)
fpos = FileGetPos(hf);
if (fpos < 0)
return FALSE;
fsize = FileGetSize(hf);
if (fsize < 0)
return FALSE;
*pcrc = 0;
if (fpos >= fsize) // Check starting location
return TRUE;
if (flen < 0) // If < 0, check to end of file
flen = fsize;
flen = MIN(flen, fsize - fpos); // Compute maximum length
cb = (SIZET)MIN((FPOS)(16 _K), flen);
pbBuf = MemAlloc(cb);
if (pbBuf == NULL)
{
ErrorNoMem();
goto ERROR_EXIT;
}
while (flen)
{
cBuf = (SIZET)MIN((FPOS)cb, flen);
if (!FileRead(hf, pbBuf, cBuf, fpos))
goto ERROR_EXIT;
*pcrc = CrcCalcAppend(pbBuf, cBuf, *pcrc);
fpos += (FPOS)cBuf;
flen -= (FPOS)cBuf;
}
fResult = TRUE;
ERROR_EXIT:
if (pbBuf)
MemFree(pbBuf);
return fResult;
}
//----------------------------------------------------------------------------
// Description: Garble a file
// Parameters: hf File handle
// pcsz String to garble file with
// flen Length of file to compute CRC for.
// Length is adjust to fit file size.
// If < 0, calculate remaing length of file.
// fpos Address to start computing at.
// If < 0, start at current position.
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
BOOL FN_E FileGarble(HF hf, PCSZ pcsz, FPOS flen, FPOS fpos)
{
BYTE bBuf[512];
SIZET cBuf;
FPOS fsize;
SIZET cStr;
Assert(pcsz && pcsz[0]);
cStr = strlen(pcsz);
if (fpos < 0)
fpos = FileGetPos(hf);
if (fpos < 0)
return FALSE;
fsize = FileGetSize(hf);
if (fsize < 0)
return FALSE;
if (fpos >= fsize) // Check starting location
return TRUE;
if (flen < 0) // If < 0, check to end of file
flen = fsize;
flen = MIN(flen, fsize - fpos); // Compute maximum length
while (flen)
{
cBuf = (SIZET)MIN((FPOS)sizeof(bBuf), flen);
if (!FileRead(hf, bBuf, cBuf, fpos))
return FALSE;
Garble(bBuf, cBuf, (PBYTE)pcsz, cStr);
if (!FileWrite(hf, bBuf, cBuf, fpos))
return FALSE;
fpos += (FPOS)cBuf;
flen -= (FPOS)cBuf;
}
return TRUE;
}
//----------------------------------------------------------------------------
//------------------------------- End of File --------------------------------
//----------------------------------------------------------------------------